package com.easylinkin.linkappapi.openapi.service.impl;

import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.easylinkin.linkappapi.device.entity.DeviceRefAreaScope;
import com.easylinkin.linkappapi.device.mapper.DeviceRefAreaScopeMapper;
import com.easylinkin.linkappapi.function.entity.TenantFunctionRefArea;
import com.easylinkin.linkappapi.function.mapper.TenantFunctionRefAreaMapper;
import com.easylinkin.linkappapi.openapi.service.ApplicationCopyDeviceService;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.space.entity.LinkappArea;
import com.easylinkin.linkappapi.space.mapper.LinkappAreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: 设备分组
 * @author: chenkaixuan
 * @create: 2021-09-07 15:05
 */
@Slf4j
@Service
public class ApplicationCopyDeviceAreaScopeServiceImpl implements ApplicationCopyDeviceService {
    @Resource
    DeviceRefAreaScopeMapper deviceRefAreaScopeMapper;
    @Resource
    LinkappAreaMapper linkappAreaMapper;
    @Resource
    TenantFunctionRefAreaMapper tenantFunctionRefAreaMapper;

    @Override
    public RestMessage copy(HashMap<String, String> screenmap, HashMap<String, String> map, LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
        if (map == null || map.size() <= 0) {
            return restMessage;
        }
        List<DeviceRefAreaScope> deviceRefAreaScopeList = deviceRefAreaScopeMapper.findAllByTenantId(oldLinkappUser.getTenantId());
        if (deviceRefAreaScopeList == null || deviceRefAreaScopeList.size() <= 0) {
            return restMessage;
        }
        //获取旧区域
        LinkappArea linkappArea = new LinkappArea();
        linkappArea.setTenantId(oldLinkappUser.getTenantId());
        List<LinkappArea> oldLinkappAreaList = linkappAreaMapper.selectLinkappAreaListAllGlobal(linkappArea);
        if (oldLinkappAreaList == null || oldLinkappAreaList.size() <= 0) {
            return restMessage;
        }
        Map<String, String> oldAreaMap = oldLinkappAreaList.stream().collect(Collectors.toMap(LinkappArea::getId, LinkappArea::getAreaPath, (k1, k2) -> k2));
        //获取新区域
        linkappArea = new LinkappArea();
        linkappArea.setTenantId(newLinkappUser.getTenantId());
        linkappArea.setAreaPaths(new ArrayList<>(oldAreaMap.values()));
        List<LinkappArea> newLinkappAreaList = linkappAreaMapper.selectLinkappAreaListAllGlobal(linkappArea);
        if (newLinkappAreaList == null || newLinkappAreaList.size() <= 0) {
            return restMessage;
        }
        Map<String, String> areaPathMap = newLinkappAreaList.stream().collect(Collectors.toMap(LinkappArea::getAreaPath, LinkappArea::getId, (k1, k2) -> k2));
        deviceRefAreaScopeList = deviceRefAreaScopeList.stream().map(m -> {
            if (map.get(m.getDeviceCode()) == null) {
                return RestBuilders.failureMessage().setMessage("设备[" + m.getDeviceCode() + "]映射关系不存在");
            }
            m.setDeviceCode(map.get(m.getDeviceCode()));
            m.setTenantId(newLinkappUser.getTenantId());
            m.setAreaId(areaPathMap.get(m.getAreaPath()));
            return m;
        }).collect(Collectors.toList());

        //复制绑定空间
        List<TenantFunctionRefArea> tenantFunctionRefAreaList = new LambdaQueryChainWrapper<>(tenantFunctionRefAreaMapper)
                .eq(TenantFunctionRefArea::getTenantId, oldLinkappUser.getTenantId())
                .list();
        if (tenantFunctionRefAreaList != null && tenantFunctionRefAreaList.size() > 0) {
            tenantFunctionRefAreaList = tenantFunctionRefAreaList.stream().map(m -> {
                m.setTenantId(newLinkappUser.getTenantId());
                m.setAreaId(areaPathMap.get(m.getAreaPath()));
                return m;
            }).collect(Collectors.toList());
        }
        //批量新增
        int batchSize = 100;
        if (deviceRefAreaScopeList != null && deviceRefAreaScopeList.size() > 0) {
            for (int idx = 0; idx < deviceRefAreaScopeList.size(); idx += batchSize) {
                List<DeviceRefAreaScope> list = deviceRefAreaScopeList.subList(idx, Math.min(deviceRefAreaScopeList.size(), idx + batchSize));
                deviceRefAreaScopeMapper.batchInsert(list);
            }
        }
        //拷贝绑定空间
        if (tenantFunctionRefAreaList != null && tenantFunctionRefAreaList.size() > 0) {
            for (int idx = 0; idx < tenantFunctionRefAreaList.size(); idx += batchSize) {
                List<TenantFunctionRefArea> list = tenantFunctionRefAreaList.subList(idx, Math.min(tenantFunctionRefAreaList.size(), idx + batchSize));
                tenantFunctionRefAreaMapper.batchInsert(list);
            }
        }
        log.info("设备分组拷贝完成");
        return restMessage;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }
}
